import _ from 'lodash'
import langs from './lang'
import {sprintf} from 'sprintf-js'

let currentLocale = langs.zhcn

/*
* 当前的Lang对象
* */
export const locale = currentLocale

/*
* 通过路径获取对应的值，如果有参数，则获取的是经过sprintf解析之后的值
* */
export const lang = (path, ...args) => {
  let template = _.get(currentLocale, path)
  if (args && args.length > 0) {
    return sprintf(template, ...args)
  } else {
    return template
  }
}

/*
* locale: string [zh-cn | en-us]
* */
export const setLocale = locale => {
  let lang = langs[locale.replace(/[- ]/g, '').toLowerCase()]
  lang && (currentLocale = lang)
}
